<template>
  <div id="app">
    <router-view></router-view>
    <el-dialog v-model="dialogVisible" title="更新提示" width="30%" :before-close="handleClose">
      <span>发现新的版本是否更新</span>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="downloadUpdate">
            更新
          </el-button>
        </span>
      </template>
      <el-progress :text-inside="true" :stroke-width="24" :percentage="percent" v-if="isUpdate"></el-progress>
    </el-dialog>
  </div>
</template>
<script>
let ipcRenderer = require('electron').ipcRenderer

export default {

  name: 'app',
  data () {
    return {
      dialogVisible: false,
      percent: 0,
      isUpdate: false
    }
  },
  mounted () {
    //接收主进程版本更新消息
    ipcRenderer.on('message', (event, arg) => {
      // for (var i = 0; i < arg.length; i++) {
      console.log('status--->', arg)
      console.log(1111, arg.cmd)
      if ('update-available' === arg.cmd) {
        // console.log(112)
        //显示升级对话框
        this.dialogVisible = true
      } else if ('download-progress' == arg.cmd) {
        this.isUpdate = true
        //更新升级进度
        console.log(arg.message.percent)
        let percent = Math.round(parseFloat(arg.message.percent))
        this.percent = percent
      } else if ('error' == arg.cmd) {
        this.dialogVisible = false
        // this.$message('更新失败')
      } else if ('update-downloaded' == arg.cmd) {
        console.log(1111, arg.cmd)
        ipcRenderer.send('update-downloaded')
        // ipcRenderer.send("quit-install")
      }
      // }
    })
    // this.timeOut = window.setTimeout(() => {
    ipcRenderer.send('checkForUpdate')
    // }, 1000)
  },
  created () {
  },
  methods: {
    handleClose () { },
    downloadUpdate () {
      console.log('触发手动跟新')
      ipcRenderer.send("confirm-downloadUpdate")
      ipcRenderer.on('update-downloaded', () => {
        ipcRenderer.send('update-downloaded')
      })
    },
  }
}
</script>
